var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
      isSubmit: false,
      applyType: '',
      dateValue: '',
      showFlag: true,
      previewImg: '',
      petImage: '',
      addcode: '',
      contactMan: '',
      contactPhone: '',
      contactWx: '',
      happenAddress: '',
      happenTime:'',
      nickName: ''
  },
  datePickerBindchange: function(e) {
      this.setData({
          dateValue: e.detail.value,
          showFlag: false
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      that.setData({
        applyType: options.applyType
      });
  },
  uploadPetImg: function(){
        var that = this;
       wx.chooseImage({
         count: 1, // 默认9
         sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
         sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
         success: function (res) {
           // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
           var tempFilePaths = res.tempFilePaths;
          that.setData({
            previewImg: tempFilePaths
          });
            that.uploadFile(that, tempFilePaths);
         }
       })
  },
  uploadFile: function(page, path) {
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
    wx.uploadFile({
      url: app.globalData.commonUrl+"/upload",
      filePath: path[0],
      name: 'img',
      header: { "Content-Type": "multipart/form-data" },
      success: function (res) {
        console.info(res);
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        }
        let data = JSON.parse(res.data).data;
        console.info('data->' + data);
        page.setData({
          petImage: data
        })
      },
      fail: function (e) {
        console.log(e);
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
        wx.hideToast(); //隐藏Toast
      }
    })
  },
  //表单提交
  formSubmit: function(e){
    let data = e.detail.value;
    if(data.petImage == ''){
      wx.showToast({
        title: '宠物图片不能为空!',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }
    if(app.trim(data.nickName) == ''){
      wx.showToast({
        title: '宠物昵称不能为空!',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }
    if(data.happenTime == ''){
      wx.showToast({
        title: '丢失时间不能为空!',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }
    if(app.trim(data.happenAddesss) == ''){
      wx.showToast({
        title: '丢失地址不能为空!',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }
    if(app.trim(data.petDesc) == ''){
      wx.showToast({
        title: '宠物描述不能为空!',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }
    if(app.trim(data.contactMan) == ''){
      wx.showToast({
        title: '联系人不能为空!',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }
    if(app.trim(data.contactPhone) == ''){
      wx.showToast({
        title: '手机号不能为空!',
        duration: 2000,
        icon: 'none'
      })
      return false;
    }
    wx.showLoading({
      title: '保存中！',
      duration: 1000
    });
    let fn = res => {
      wx.hideLoading();
      if(res.data.code == 200){
        wx.showToast({
          title: '发布成功!',
          duration: 1000
        });
        wx.navigateBack({
            delta: 1
        })
      }
    }
    app.ajax('/petLost/add', 'post', JSON.stringify(data), fn)
  }
})